<script setup>

import {getCurrentInstance, ref} from "vue";
import api from "../../request/apis.js";
import {Button, Input, Space} from "view-ui-plus";
import moment from "moment";


const {proxy} = getCurrentInstance();

const agentInvokeUrl = ref("http://172.27.13.16:10240/agent/medicare/execute");

const outHospitalRevokeParam = ref("");

const outHospitalRevokeFlag = ref(false);
const outHospitalRevokeFunc = async () => {

  const body =
      {
        "dllName": "MedicareGuiZhou",
        "funcName": "business",
        "outPos": 3,
        "params": [
          "26",
          outHospitalRevokeParam.value
        ]
      };

  const data = await api.outHospitalRevoke(agentInvokeUrl.value, body);
  proxy.$Message.info(data.output);

}
const startDate = ref(new Date(moment().format("YYYY-MM-DD")));

const endDate = ref(new Date(moment().format("YYYY-MM-DD")));

const columns = ref([
  {
    title: '序号',
    key: 'sortNo',
    type: 'index'
  },
  {
    title: '交易申请时间',
    key: 'tradeApplyTime',
    slot: 'tradeApplyTime',
  },
  {
    title: '交易结果',
    key: 'tradeStatusName'
  },
  {
    title: '患者姓名',
    key: 'patientName'
  },
  {
    title: '交易编号',
    key: 'tradeCode'
  },
  {
    title: '入参信息',
    key: 'paramIn',
    tooltip: true,
  },
  {
    title: '出参信息',
    key: 'paramOut',
    tooltip: true,
  },
  {
    title: '操作',
    key: 'action',
    slot: 'action'
  },
]);

const patientName = ref();
const data = ref([]);
const queryMedicareTradeLog = async () => {

  if (!patientName.value) {
    proxy.$Message.info("请输入患者姓名");
    return
  }

  const logs = await api.queryMedicareTradeLog(
      {
        patientName: patientName.value,
        startTime: new Date(moment(startDate.value).format("YYYY-MM-DD")).getTime(),
        endTime: new Date(moment(endDate.value).format("YYYY-MM-DD") + " 23:59:59").getTime()
      }
  );
  data.value = logs.records;
}

const back = (row) => {
  outHospitalRevokeParam.value = row.paramIn;
}
</script>

<template>
  <Layout>
    <Content class="layout-content">
      <Row>
        <Col span="24">

          <Space direction="vertical" size="large" type="flex">

            <Row>

              <Col span="2">

                <Input placeholder="请输入患者姓名" v-model="patientName"></Input>

              </Col>
              &nbsp; &nbsp; &nbsp;

              <Col span="3">

                <DatePicker type="date" v-model="startDate" placeholder="Select date" style="width: 200px"/>

              </Col>
              &nbsp; &nbsp; &nbsp;

              <Col span="3">

                <DatePicker type="date" v-model="endDate" placeholder="Select date" style="width: 200px"/>

              </Col>
              &nbsp; &nbsp; &nbsp;

              <Col span="10">

                <Button type="primary" @click="queryMedicareTradeLog">查询</Button>

              </Col>
            </Row>
            <Row>
              <Col span="24">
                <Table height="400" stripe border highlight-row :columns="columns" :data="data">
                  <template #tradeApplyTime="{ row, index }">
                    {{ moment(new Date(parseInt(row.tradeApplyTime))).format("YYYY-MM-DD HH:MM:SS") }}
                  </template>

                  <template #action="{ row, index }">
                    <Space wrap>

                      <Button type="primary" v-if="row.tradeCode==26||row.tradeCode==25" @click="back(row)">
                        回退
                      </Button>

                    </Space>
                  </template>

                </Table>
              </Col>
            </Row>
            <Row>
              <Col span="24">
                入参：
                <Input v-model="outHospitalRevokeParam" show-word-limit type="textarea"
                       :rows="10"
                       placeholder="请输入26交易入参"/>

                <Button type="primary" @click="outHospitalRevokeFlag=true">
                  确认
                </Button>
                <Modal
                    v-model="outHospitalRevokeFlag"
                    title="医保交易"
                    @on-ok="outHospitalRevokeFunc"
                    @on-cancel="outHospitalRevokeFlag=false">
                  <p>确认进行出院回退吗？</p>
                </Modal>
              </Col>
            </Row>

          </Space>

        </Col>

      </Row>
    </Content>
  </Layout>
</template>

<style scoped>

</style>